package com.yuandengta.yomall.search.controller;


import com.yuandengta.common.exception.BizCode;
import com.yuandengta.common.to.es.SkuEsModel;
import com.yuandengta.common.utils.R;
import com.yuandengta.yomall.search.service.ProductService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 *
 * 将商品保存到es
 * 查询从es 里面
 *
 */
@Api(value = "保存商品服务到es", tags = "商品处理Elastic")
@Slf4j
@RequestMapping("/search/product")
@RestController
public class ElasticProductController {

    @Autowired
    ProductService productService;

    @ApiResponses({
            @ApiResponse(code = 400, message = "请求无效"),
            @ApiResponse(code = 401, message = "请求参数没填好"),
            @ApiResponse(code = 403, message = "禁止访问"),
            @ApiResponse(code = 404, message = "无法找到接口"),
            @ApiResponse(code = 408, message = "请求超时"),
            @ApiResponse(code = 503, message = "接口错误,联系接口开发者"),
    })
    @ApiOperation(value = "商品上架", notes = "针对ResponseEntity<T>类型")
    @PostMapping("/save")
    public R productUp(@RequestBody List<SkuEsModel> skuEsModelList) {

        boolean flag = false;
        try {
            flag = productService.productUp(skuEsModelList);
        } catch (Exception e) {
            return R.error(BizCode.PRODUCT_UP_EXCEPTION.getCode(), BizCode.PRODUCT_UP_EXCEPTION.getMessage());
        }
        if (!flag) {
            return R.ok();
        } else {
            return R.error(BizCode.PRODUCT_UP_EXCEPTION.getCode(), BizCode.PRODUCT_UP_EXCEPTION.getMessage());
        }

    }

    @ApiOperation(value = "测试hello", notes = "调试用")
    @GetMapping("/hello")
    public R hello() {
        return R.ok("hello");
    }

    @ApiResponses({
            @ApiResponse(code = 400, message = "请求无效"),
            @ApiResponse(code = 401, message = "请求参数没填好"),
            @ApiResponse(code = 403, message = "禁止访问"),
            @ApiResponse(code = 404, message = "无法找到接口"),
            @ApiResponse(code = 408, message = "请求超时"),
            @ApiResponse(code = 503, message = "接口错误,联系接口开发者"),
    })
    @ApiOperation(value = "商品下架", notes = "针对ResponseEntity<T>类型")
    @PostMapping("/del")
    public R productDown(@RequestBody List<SkuEsModel> skuEsModelList) {

        boolean flag = false;
        try {
            flag = productService.productDown(skuEsModelList);
        } catch (Exception e) {
            return R.error(BizCode.PRODUCT_UP_EXCEPTION.getCode(), BizCode.PRODUCT_UP_EXCEPTION.getMessage());
        }
        if (!flag) {
            return R.ok();
        } else {
            return R.error(BizCode.PRODUCT_UP_EXCEPTION.getCode(), BizCode.PRODUCT_UP_EXCEPTION.getMessage());
        }

    }

    @ApiOperation(value = "简单搜索")
    @RequestMapping(value = "/search/simple", method = RequestMethod.GET)
    @ResponseBody
    public R search(@RequestParam(required = false) String keyword,
                    @RequestParam(required = false, defaultValue = "0") Integer pageNum,
                    @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        try {
            return productService.search(keyword, pageNum, pageSize);
        } catch (IOException e) {
            return R.error("查询失败");
        }
    }

    @ApiOperation(value = "综合搜索、筛选、排序")
    @ApiImplicitParam(name = "sort", value = "排序字段:0->按相关度；1->按新品；2->按销量；3->价格从低到高；4->价格从高到低",
            defaultValue = "0", allowableValues = "0,1,2,3,4", paramType = "query", dataType = "integer")
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    @ResponseBody
    public R search(@RequestParam(required = false) String keyword,
                    @RequestParam(required = false) Long brandId,
                    @RequestParam(required = false) Long productCategoryId,
                    @RequestParam(required = false, defaultValue = "0") Integer pageNum,
                    @RequestParam(required = false, defaultValue = "5") Integer pageSize,
                    @RequestParam(required = false, defaultValue = "0") Integer sort) {
        return productService.search(keyword, brandId, productCategoryId, pageNum, pageSize, sort);
    }
}
